package com.dongdongshop.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.dongdongshop.pojo.Cart;
import com.dongdongshop.pojo.TbItem;
import com.dongdongshop.pojo.TbOrderItem;
import com.dongdongshop.service.CartService;
import com.dongdongshop.service.ItemService;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
@Component
public class CartServiceImpl implements CartService {
    @Reference(check=false ,timeout=106666)
    private ItemService itemService;

    @Override
    public List<Cart> addToCart(List<Cart> cartList, Long itemId, Integer num) {
        //1、根据商品sku ID查询sku商品信息
        TbItem item = itemService.selectItemById(itemId);
        //2、获取商家id
        String sellerId = item.getSellerId();
        //3、根据商家id判断购物车列表中是否存在该商家的购物车
        Cart ct = searchCartBySellerId(sellerId,cartList);
        //4、如果购物车列表中不存在该商家的购物车
        if (ct == null){
            ct = new Cart();
            ct.setSellerId(sellerId);
            ct.setSellerName(item.getSeller());
            //将商品的sku数据保存到订单明细对象中
            TbOrderItem orderItem = getTbOrderItem(item, num);
            List<TbOrderItem> orderItemList = new ArrayList<>();
            orderItemList.add(orderItem);
            ct.setOrderItemList(orderItemList);
            //将新建的购物车对象添加到购物车列表
            cartList.add(ct);
        }else { //如果购物车列表中存在该商家的购物车
            //查询购物车明细列表中是否存在改商品
            List<TbOrderItem> orderItemList = ct.getOrderItemList();
            TbOrderItem tbOrderItem = searchTbOrderItemById(orderItemList, item);
            //如果没有，新增购物车明细
            if (tbOrderItem == null){
                tbOrderItem = getTbOrderItem(item,num);
                ct.getOrderItemList().add(tbOrderItem);
            }else {
                //如果有，在原购物车明细上添加数量，更改金额
                tbOrderItem.setNum(tbOrderItem.getNum() +num);
                //总价格
                tbOrderItem.setTotalFee(new BigDecimal(tbOrderItem.getNum() * tbOrderItem.getPrice().doubleValue()));
                //如果数量加完只是num <=0 。我们就把她移除掉，而不是继续保留在集合中
                if (tbOrderItem.getNum() <= 0){
                    ct.getOrderItemList().remove(tbOrderItem);
                }
                //如果移除后orderItemList大小为0了，那么我们把这个cart移除掉
                if (ct.getOrderItemList().size() <= 0){
                    cartList.remove(ct);
                }
            }
        }
        return cartList;
    }

    //根据商家id查询购物车列表中商家的购物车
    public Cart searchCartBySellerId(String sellerId , List<Cart> cartList){
        return cartList.stream().filter(x -> x.getSellerId().equals(sellerId)).findFirst().orElse(null);
        /*for (Cart cart : cartList) {
            if (cart.getSellerId().equals(sellerId)){
                return cart;
            }
        }
        return null;*/
    }
    //将商品的数据保存到订单对象中
    public TbOrderItem getTbOrderItem(TbItem item,Integer num){
        TbOrderItem tbOrderItem = new TbOrderItem();
        tbOrderItem.setGoodsId(item.getGoodsId());
        tbOrderItem.setItemId(item.getId());
        tbOrderItem.setNum(num);
        tbOrderItem.setPicPath(item.getImage());
        tbOrderItem.setPrice(item.getPrice());
        tbOrderItem.setTotalFee(new BigDecimal(item.getPrice().doubleValue() * num));//总价格
        tbOrderItem.setTitle(item.getTitle());
        tbOrderItem.setSellerId(item.getSellerId());
        return tbOrderItem;
    }
    //根据商品的id查询购物车明细中商品的数据
    public TbOrderItem searchTbOrderItemById(List<TbOrderItem> orderItemList,TbItem item){
        /*for (TbOrderItem t0.2bOrderItem : orderItemList) {
            if (tbOrderItem.getItemId().equals(item.getId())){
                return tbOrderItem;
            }

        }
        return null;*/
        return orderItemList.stream().filter(x -> x.getItemId().equals(item.getId())).findFirst().orElse(null);
    }
}
